<script type="text/javascript"><!--//--><![CDATA[//><!--
var LOVIsp_Produk = function(ao_option){
	try{
		if(ao_option==undefined){ ao_option={}; }
		var lf_onSelect	= (ao_option.onSelect==undefined || typeof(ao_option.onSelect)!='function') ? function(){} : ao_option.onSelect;
		
		// Create Popup
		var $popup	= openPopup({ title:'Daftar Produk' , width:700, height:370 });
		var ls_id	= $popup.attr('id');
		
		// Append Datagrid Container
		var ls_gridName	= 'grid_'+ls_id;
		$popup	.html(	$('<div>')	.attr('id', 'tb'+ls_id).attr('style','padding:5px;height:auto;')
									.html(	$('<div>').html($('<form>')	.attr('id','fm_search_'+ls_id).data('rel',ls_id).submit(function(e){ $('#'+ls_gridName).datagrid('reload', $('#fm_search_'+$(this).data('rel')).serializeObject()); e.preventDefault(); })
																		.html('<table class="tb_form" style="margin-bottom:0px"><tr><td width="100">Keyword</td><td><input type="text" name="keyword" class="textfield medium" /><a href="#" class="easyui-linkbutton" iconCls="icon-search" id="btn_search'+ls_id+'" rel="'+ls_id+'">Search</a></td></tr></table>')
															)
											)
						)
				.append($('<div>').attr('id', ls_gridName).attr('toolbar', '#tb'+$popup.attr('id')));
		
		$('#btn_search'+ls_id).click(function(){ $('#'+ls_gridName).datagrid('reload', $('#fm_search_'+$(this).attr('rel')).serializeObject()); return false; });
		$('#btn_search'+ls_id).linkbutton({  
			plain:true  
		}); 
		
		// Build Datagrid
		var ls_param = (ao_option.param==undefined ? '' : ao_option.param);
		$('#'+ls_gridName).datagrid({  
			url:gs_path+'/isp_inventori_turn_over/getdata2/'+ls_param,
			queryParams: $('#fm_search_'+ls_id).serializeObject(),
			height:337,
			pagination:true,
			singleSelect:true,
			columns:[[
				{ field : 'kode_inventori', title : 'Kode Produk', width : 120, sortable:true },
				{ field : 'nama_inventori', title : 'Nama Produk', width : 170, sortable:true },
				{ field : 'banyak', title : 'Stock', width : 100, sortable:true },
				//{ field : 'harga', title : 'Harga Satuan', width : 170, sortable:true },
			]],
			onSelect:function(rowIndex, rowData){ lf_onSelect(rowData); closePopup(); }
		});
		
	} catch (err) { alert('Func. LOVSampleReference :\n' + err); }
};
var LOVKontrakInv = function(ao_option){
	try{
		if(ao_option==undefined){ ao_option={}; }
		var lf_onSelect	= (ao_option.onSelect==undefined || typeof(ao_option.onSelect)!='function') ? function(){} : ao_option.onSelect;
		
		// Create Popup
		var $popup	= openPopup({ title:'Daftar Kontrak' , width:700, height:370 });
		var ls_id	= $popup.attr('id');
		
		// Append Datagrid Container
		var ls_gridName	= 'grid_'+ls_id;
		$popup	.html(	$('<div>')	.attr('id', 'tb'+ls_id).attr('style','padding:5px;height:auto;')
									.html(	$('<div>').html($('<form>')	.attr('id','fm_search_'+ls_id).data('rel',ls_id).submit(function(e){ $('#'+ls_gridName).datagrid('reload', $('#fm_search_'+$(this).data('rel')).serializeObject()); e.preventDefault(); })
																		.html('<table class="tb_form" style="margin-bottom:0px"><tr><td width="100">Keyword</td><td><input type="text" name="keyword" class="textfield medium" /><a href="#" class="easyui-linkbutton" iconCls="icon-search" id="btn_search'+ls_id+'" rel="'+ls_id+'">Search</a></td></tr></table>')
															)
											)
						)
				.append($('<div>').attr('id', ls_gridName).attr('toolbar', '#tb'+$popup.attr('id')));
		
		$('#btn_search'+ls_id).click(function(){ $('#'+ls_gridName).datagrid('reload', $('#fm_search_'+$(this).attr('rel')).serializeObject()); return false; });
		$('#btn_search'+ls_id).linkbutton({  
			plain:true  
		}); 
		
		// Build Datagrid
		var ls_param = (ao_option.param==undefined ? '' : ao_option.param);
		$('#'+ls_gridName).datagrid({  
			url:gs_path+'/isp_inventori_turn_over/getdata3/'+ls_param,
			queryParams: $('#fm_search_'+ls_id).serializeObject(),
			height:337,
			pagination:true,
			singleSelect:true,
			columns:[[
				{ field : 'no_id', title : 'ID Kontrak', width : 170, sortable:true },
				{ field : 'kode_kontrak', title : 'Kode Kontrak', width : 170, sortable:true },
				{ field : 'kode_customer', title : 'Kode Client', width : 170, sortable:true },
				{ field : 'nama_customer', title : 'Nama Client', width : 170, sortable:true },
			]],
			onSelect:function(rowIndex, rowData){ lf_onSelect(rowData); closePopup(); }
		});
		
	} catch (err) { alert('Func. LOVSampleReference :\n' + err); }
};

	var grid_reload = function(){
		$('#datagrid').datagrid('reload', $('#fm_search').serializeObject());
	};

	$(document).ready(function() {

		// Setup Datagrid
		$('#datagrid').datagrid({  
			url:gs_path+'/'+controller+'/getdata', 
			queryParams: $('#fm_search').serializeObject(),
			height:340,
			pagination:true,
			columns:[[
				{ field : 'ck', checkbox:true, width: 40, align: 'center' },
				{ field : 'kode_kontrak', title : 'Kode Kontrak', width : 150, sortable:true },
				{ field : 'tgl_dipinjam', title : 'Tanggal Dipinjam', width : 150, sortable:true },
				//{ field : 'banyak', title : 'Stock', width : 120, sortable:true },
				{ field : 'kode_barang', title : 'Kode Produk', width : 120, sortable:true },
				{ field : 'nama_barang', title : 'Nama Produk', width : 120, sortable:true },
				{ field : 'status', title : 'Status', width : 150, sortable:true },
				{ field : 'keterangan', title : 'Keterangan', width : 150, sortable:true }
			]]
		});		
		
		// Setup Form
		$('#form_master').setupForm([			
			{ name: 'kode_barang', type: 'lov', size: 'medium', noempty:true,
				onClearButton:function(){
					$(this).setval(''); 
					$('#nama_barang').setval('');
				
				},
				onActionButton:function(){ 
					LOVIsp_Produk({ onSelect:function(row){
						$('#id_inventori').setval(row.id_inventori); 
						$('#kode_barang').setval(row.kode_inventori); 
						$('#nama_barang').setval(row.nama_inventori);
						$('#banyak').setval(row.banyak);
					} }); 
				}
			},
			{ name: 'banyak', type: 'text', size: 'medium'},
			{ name: 'jumlah', type: 'text', size: 'medium', noempty:true},
			{ name: 'nama_barang', type: 'text', size: 'long'},
			{ name: 'tgl_dipinjam', type: 'date', size: 'medium', noempty:true},
			{ name: 'kode_kontrak', type: 'lov', size: 'medium', noempty:true,
				onClearButton:function(){
					$(this).setval(''); 
					$('#kode_customer').setval('');
					$('#nama_customer').setval('');
				
				},
				onActionButton:function(){ 
					LOVKontrakInv({ onSelect:function(row){
						$('#id_kontrak').setval(row.no_id);
						$('#kode_kontrak').setval(row.kode_kontrak);
						$('#kode_customer').setval(row.kode_customer);
						$('#nama_customer').setval(row.nama_customer); 
					} }); 
				}
			},
			{ name: 'kode_customer', type: 'text', size: 'long', maxlength: 255, noempty:true},
			{ name: 'nama_customer', type: 'text', size: 'long', maxlength: 255, noempty:true},
			{ name: 'status', type: 'text', size: 'medium', noempty:true },
			{ name: 'keterangan', type: 'textarea', size: 'medium' },
		]);
		
		
		// Create Dialog
		$('#dialog_container').dialog({
			closed:true,
			modal:true,
			title: '_',
			width:600,
			buttons:[{
				text:'Save',
				iconCls:'icon-save',
				handler:function(){
					$('#form_master').submitForm('/'+controller+'/ajax_post', { onComplete : function(){ $('#dialog_container').dialog('close'); $('#datagrid').datagrid('reload'); }} );
					//loadPage(gs_path + '/' + controller + '/index/');
				}
			},{
				text:'Close',
				iconCls:'icon-cancel',
				handler:function(){
					$('#dialog_container').dialog('close');
				}
			}]
		});
		
		
		// Set Action Button
		var buttons = [ { label: 'Add', icon: 'add', onclick: function() {
							resetError();
							$.ajax({
								url:gs_path+'/'+controller+'/id_auto',
								type:'POST',
								dataType:'JSON',
								}).done(function(data){
									$('#id_peminjaman').setval(data['id_peminjaman_auto']);
							});
							$('#dialog_container').dialog('open');
							$('#form_master').data('mode', 'create');
							$('#form_master').parent().parent().parent().prev().find('.panel-title').html('New Peminjaman Inventori');
							$('#kode_barang, #banyak, #nama_barang, #tgl_dipinjam, #kode_kontrak, #status, #keterangan, #banyak, #jumlah, #kode_customer, #nama_customer').val('');	
							$('#kode_barang').focus();
							$('#banyak, #nama_barang, #kode_customer,#nama_customer').readonly();
							$('#kode_kontrak').readonly(false);
                            $('#jumlah').keyup(function() {
                                var jumlah =  parseInt($('#jumlah').val());
                                var banyak = parseInt($('#banyak').val());
                                var j = banyak + 1;
                                //alert(banyak);
                                if(jumlah>=j){
                                    alert(ls_eror ='input jumlah melebihi stock !\n');
                                    $('#jumlah').setval('0');
                                    $('#jum_akhir').setval('');
                                } else {
                                    var jum_akhir = banyak - jumlah;
                                    //\alert(jum_akhir);
                                    $('#update_stock').setval(jum_akhir);

                                }
                            });
						} },
						{ label: 'Edit', icon: 'edit', onclick: function() {
							var oSel=$('#datagrid').datagrid('getSelected'); 
							if(oSel==null){ alert('Please select row!'); }
							else{ 
								resetError();
								$('#dialog_container').dialog('open'); 
								$('#form_master').data('mode', 'edit');
								$('#form_master').parent().parent().parent().prev().find('.panel-title').html('Edit Peminjaman Inventori');
								$('#kode_kontrak, #nama_customer, #kode_customer, #kode_barang, #nama_barang, #banyak').readonly(true);
								jsonToForm(oSel); $('#kode_kontrak').focus();
								$('#kode_barang, #banyak, #nama_barang, #kode_kontrak, #kode_customer,#nama_customer').readonly();
								$('#id_peminjaman').setval(oSel['id_peminjaman']);
								//alert(oSel['id_peminjaman']);
								$('#update_stock').setval(oSel['banyak']);

								//Calculation Keyup Function
									$('#jumlah').keyup(function() {
										var jum_awal =  parseInt(oSel['jumlah']);
										var jumlah =  parseInt($('#jumlah').val());
										var banyak = parseInt($('#banyak').val());
										var ls_eror = '';
										//alert(jum_awal);
										$('#jum_awal').setval(jum_awal);
											var jum_akhir = jum_awal - jumlah;
											$('#jum_akhir').setval(jum_akhir);	

											if(jum_awal<=jumlah){
												var jum_cal = jum_akhir *= (-1) ;
												//alert(jum_akhir);
												var j = banyak + 1;
												$('#jum_akhir').setval(jum_cal);
													if(j<=jum_cal){
														alert(ls_eror ='input jumlah melebihi stock !\n');
														$('#jumlah').setval(jum_awal);
														$('#jum_akhir').setval('');
													} /*else if(jum_akhir==0){
                                                        var update_stock = jumlah_barang - banyak;
                                                        $('#update_stock').setval(update_stock);
                                                    }*/ else {
                                                        var update_stock = banyak - jum_cal;
                                                        $('#update_stock').setval(update_stock);
                                                    }
											} else if(jum_awal>=jumlah) {
												//alert(jum_akhir);
												$('#jum_akhir').setval(jum_akhir);
												var update_stock = banyak + jum_akhir;
												$('#update_stock').setval(update_stock);
											}
									});
							} 
						} },
						{label: 'Delete', icon: 'delete', onclick: function() { 
							var oSel = $('#datagrid').datagrid('getSelected'); 
							if (oSel == null) { 
								$.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); 
							} else { 
								deleteData(['id_peminjaman']);
							} 
						}}];
		setAction(buttons);
		
		
		$('#fm_search').focusFirst();
	});
//--><!]]></script>

<div id="tb" style="padding:5px;height:auto">
    <div>
        <form id="fm_search" onsubmit="grid_reload(); return false;">
        	<table class="tb_form" style="margin-bottom:0px">
            <tr>
            	<td width="100">Keyword</td>
                <td><input type="text" id="keyword" name="keyword" class="textfield medium" />
                	<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="grid_reload(); return false;">Search</a>
                </td>
            </tr>
            </table>
        </form>
    </div>
</div>
<div id="datagrid" toolbar="#tb"></div>



<div id="dialog_container" style="padding:5px;width:600px;height:380px;">
	<form id="form_master">
    <table class="tb_form" style="width:100%;" cellpadding="0" cellspacing="0">
    <tr>
        <td>
        	<input id="id_peminjaman" name="id_peminjaman" type="hidden" />
        	<input id="id_kontrak" name="id_kontrak" type="hidden" />
        	<input id="id_inventori" name="id_inventori" type="hidden" />
        </td>
    </tr>
    <tr>
        <td class="label" style="width:150px">Kode Produk</td>
        <td><input id="kode_barang" /></td>
    </tr>
    <tr>
        <td class="label">Nama Produk</td>
        <td><input id="nama_barang" /></td>
    </tr>
    <tr>
        <td class="label">Stock</td>
        <td><input id="banyak" /><input id="update_stock" name="update_stock" type="hidden"/></td>
    </tr>
    <tr>
        <td class="label">Jumlah Pinjaman</td>
        <td><input id="jumlah" name="jumlah" /><input id="jum_awal" name="jum_awal"  type="hidden"/><input id="jum_akhir" name="jum_akhir"  type="hidden"/></td>
    </tr>
    <tr>
        <td class="label">Tanggal Dipinjam</td>
        <td><input id="tgl_dipinjam" /></td>
    </tr>  
    <tr>
        <td class="label">Kode Kontrak</td>
        <td><input id="kode_kontrak" /></td>
    </tr>
    <tr>
        <td class="label">Kode Client</td>
        <td><input id="kode_customer" /></td>
    </tr>
    <tr>
        <td class="label">Nama Client</td>
        <td><input id="nama_customer" /></td>
    </tr>  
    <tr>
        <td class="label">Status</td>
        <td><input id="status" /></td>
    </tr>  
    <tr>
        <td class="label">Keterangan</td>
        <td><input id="keterangan" /></td>
    </tr>     
    </table>
	</form>
</div>